<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-form-item label="监督任务" v-bind="validateInfos.taskName">
        <a-input v-model:value="formData.taskName" placeholder="请输入监督任务" :disabled="disabled"></a-input>
      </a-form-item>
      <a-form-item label="受检单位" v-bind="validateInfos.entName">
        <a-input v-model:value="formData.entName" placeholder="请输入受检单位" :disabled="disabled"></a-input>
      </a-form-item>
      <a-form-item label="抽样单位" v-bind="validateInfos.spotEntName">
        <a-input v-model:value="formData.spotEntName" placeholder="请输入抽样单位" :disabled="disabled"></a-input>
      </a-form-item>
      <a-form-item label="接收人员" v-bind="validateInfos.receiver">
        <a-select v-model:value="formData.receiver" :options="optionReceiver" placeholder="请选择接收人员" :disabled="disabled"></a-select>
      </a-form-item>
      <a-form-item label="经办人" v-bind="validateInfos.handler">
        <a-input v-model:value="formData.handler" placeholder="请输入经办人" :disabled="disabled"></a-input>
      </a-form-item>
      <a-form-item label="经办日期" v-bind="validateInfos.handlDate">
        <a-date-picker v-model:value="formData.handlDate" placeholder="请输入经办日期" :disabled="disabled" :format="dateFormat" />
      </a-form-item>
      <a-form-item label="任务附件" v-bind="validateInfos.taskAtta">
        <JUpload v-model:value="formData.taskAtta" :disabled="disabled"></JUpload>
      </a-form-item>

    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
import { ref, reactive, defineExpose, nextTick, defineProps, computed } from 'vue';
import JUpload from '/@/components/Form/src/jeecg/components/JUpload/JUpload.vue';
import { useMessage } from '/@/hooks/web/useMessage';
import { saveOrUpdate, getReceiverList } from '../supervisionTasksList.api';
import { Form } from 'ant-design-vue';
import { useUserStore } from '/@/store/modules/user';
const { userInfo } = useUserStore();
import dayjs from 'dayjs';

const props = defineProps({
  formDisabled: { type: Boolean, default: false },
  formData: { type: Object, default: () => { } },
  formBpm: { type: Boolean, default: true },
  auditMark: { type: String, default: '' },
});

const optionReceiver = ref<any>([]);
const formRef = ref();
const useForm = Form.useForm;
const emit = defineEmits(['register', 'ok']);
const dateFormat = 'YYYY-MM-DD'
const formData = reactive<Record<string, any>>({
  createBy: "",
  createTime: "",
  entName: "",
  handlDate: dayjs(),
  handler: userInfo.realname,
  id: "",
  receiveId: "",
  receiver: "",
  spotEntName: "天研院气田开发化学实验室",
  synFlag: "",
  sysOrgCode: "",
  taskAtta: "",
  taskName: "",
  updateBy: "",
  updateTime: ""
});
const { createMessage } = useMessage();
const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
const confirmLoading = ref<boolean>(false);
//表单验证
const validatorRules = {
  taskName: [{ required: true, message: '监督任务不能为空！', trigger: 'blur' }],
  entName: [{ required: true, message: '受检单位不能为空！', trigger: 'blur' }],
  spotEntName: [{ required: true, message: '抽样单位不能为空！', trigger: 'blur' }],
  receiver: [{ required: true, message: '接收人员不能为空！', trigger: 'blur' }],
  handler: [{ required: true, message: '经办人不能为空！', trigger: 'blur' }],
  handlDate: [{ required: true, message: '经办日期不能为空！', trigger: 'blur' }],
  taskAtta: [{ required: true, message: '任务附件不能为空！', trigger: 'blur' }],
};
const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

const getReceiverListOptions = async ()=>{
  const res = await getReceiverList();
  if(res.success&&res.result.records.length>0){
    res.result.records.forEach(element => {
      optionReceiver.value.push({
        value:element.id,
        label:element.realname,
      }) 
    });    
  }  
}
getReceiverListOptions();
// 表单禁用
const disabled = computed(() => {
  if (props.formBpm === true) {
    if (props.formData.disabled === false) {
      return false;
    } else {
      return true;
    }
  }
  return props.formDisabled;
});

/**
 * 新增
 */
function add() {
  edit({});
}

/**
 * 编辑
 */
function edit(record) {
  nextTick(async () => {
    resetFields();
    //赋值
    Object.assign(formData, record);
    if(formData.handlDate){
      formData.handlDate=dayjs(formData.handlDate)
    }
  });
}
/**
 * 提交数据
 */
async function submitForm() {
  // 触发表单验证
  await validate();
  confirmLoading.value = true;
  const isUpdate = ref<boolean>(false);
  //时间格式化
  let model = formData;
  if (formData.id != null && formData.id != '') {
    isUpdate.value = true;
  }
  await saveOrUpdate(model, isUpdate.value)
    .then((res) => {
      if (res.success) {
        createMessage.success(res.message);
        emit('ok');
      } else {
        createMessage.warning(res.message);
      }
    })
    .finally(() => {
      confirmLoading.value = false;
    });
}

defineExpose({
  add,
  edit,
  submitForm,
  formData,
});
</script>

<style lang="less" scoped>
.antd-modal-form {
  overflow-y: auto;
  padding: 24px 24px 24px 24px;
}
.ant-picker{
  width:100%;
}

.signBox {
  display: flex;
  width: 100%;
}

.signBoxLabel {
  margin-right: 50px;
  display: flex;
  padding-left: 20px;
}

.signImg {
  width: 200px;
}
</style>
